from itsdangerous import TimedJSONWebSignatureSerializer as SE
from django.conf import settings


# 加密
def generate_access_token(openid):
    se = SE(secret_key=settings.SECRET_KEY,expires_in=3600) # 秘钥  过期时间

    b = se.dumps(openid)

    return b.decode()


# 解密

def check_access_token(access_token):
    se = SE(secret_key=settings.SECRET_KEY,expires_in=3600) # 秘钥  过期时间

    try:
        b = se.loads(access_token)
    except Exception:
        return None
    else:
        return b

# ****************************************************
# 对邮箱token加密

def generate_email_verify_token(userid):
    se = SE(secret_key=settings.SECRET_KEY,expires_in=3600) # 秘钥  过期时间

    b = se.dumps(userid)

    return b.decode()


# 解密

def check_email_token(token):
    se = SE(secret_key=settings.SECRET_KEY,expires_in=3600) # 秘钥  过期时间

    try:
        b = se.loads(token)
    except Exception:
        return None
    else:
        return b
